import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

//3.x.x 也可以生效
// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }

Vue.use(VueRouter);
//import store from '@/store/index.js'

const routes = [
  {
    path: "/home",
    name: "Home",
    component: Home,
  },
  {
    path: "/",
    redirect:'/home'
  },
  {
    path: "/list",
    name: "List",
    component: () =>
      import("../views/List.vue"),
  },
  {
    path: "/cart",
    name: "Cart",
    component: () =>
      import("../views/Cart.vue"),
  },
  {
    path: "/my",
    name: "My",
    component: () =>
      import("../views/My.vue"),
  },
  {
    path: "/search",
    name: "Search",
	children:[
		{
			path:'/',
			name:'index',
			component: () =>
			  import("../views/search/SearchIndex.vue"),
		},
		{
			path:'list',
			name:'list',
			component: () =>
			  import("../views/search/SearchList.vue"),
		}
	],
    component: () =>
      import("../views/Search.vue"),
    },
    {
      path: "/login",
      name: "Login",
      component: () =>
        import("../views/login/Login.vue"),
    },
    {
      path: "/pwdLogin",
      name: "PwdLogin",
      component: () =>
        import("../views/login/PwdLogin.vue"),
    },
    {
      path: "/register",
      name: "Register",
      component: () =>
        import("../views/login/Register.vue"),
    },
    {
      path: "/recovery",
      name: "Recovery",
      component: () =>
        import("../views/login/Recovery.vue"),
    },
    {
      path: "/recoveryNext",
      name: "RecoveryNext",
      component: () =>
        import("../views/login/RecoveryNext.vue"),
    },
    {
      path: "/detail",
      name: "Detail",
      component: () =>
        import("../views/Detail.vue"),
    },
    {
        path: "/path",
        name: "Path",
        children:[
            {
                path:'/',
                name:'path-index',
                component: () =>
                  import("../views/path/Path-index.vue"),
            },
            {
                path:'path-list',
                name:'path-list',
                component: () =>
                  import("../views/path/Path-list.vue"),
            }
        ],
    component: () =>
        import("../views/Path.vue"),
    },
    {
      path: "/order",
      name: "Order",
      component: () =>
        import("../views/Order.vue"),
    },
    {
      path: "/success",
      name: "Success",
      component: () =>
        import("../views/Success.vue"),
    },
];


const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

/*

router.beforeEach((to,from,next)=>{
    //老版本router
    
    //获取到登录的状态
    let token = store.state.user.token;
    //哪些页面要做验证
    let nextRoute = ['Cart','path-index'];
    
    if(  nextRoute.indexOf(to.name) >= 0  ){
        //没有登录
        if( !token ){
           router.push('/login');
        }else{
            next();
        }
    }else{
        let loginName = ['Login','PwdLogin'];
        if(  loginName.indexOf(to.name) >= 0  ){
            let userInfo = JSON.parse( localStorage.getItem('userInfo') );
            if( userInfo ){
                router.push('/home');
            }else{
                next();
            }
        }else{
            next();
        }
    }
    next();
    
    
    //新版本router
   
    let token = store.state.user.token;
    //哪些页面要做验证
    let nextRoute = ['Cart'];
    if(  nextRoute.indexOf(to.name) >= 0  ){
        //没有登录
        if( !token ){
           router.push('/login');
        }
    }
    
    let loginName = ['Login','PwdLogin'];
    if(  loginName.indexOf(to.name) >= 0  ){
        let userInfo = JSON.parse( localStorage.getItem('userInfo') );
        if( userInfo ){
            router.push('/home');
        }
    }
    next();
    
})
*/


export default router;
